package com.crealth.memcached;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.crealth.db.ConsumerDb;
import com.crealth.model.ConsumerInfoModel;


public class ConsumerCached {
	private static Logger errorLog = Logger.getLogger("error_log");
	private static final String keyPrefix = "consumer_id_";
	
	public static ConsumerInfoModel selectConsumer(int consumerID) {
		String key = keyPrefix + consumerID;
		Object obj = MemcachedMgr.cachedClient.get(key);
		ConsumerInfoModel consumer = null;
		if (obj != null) {
			consumer = (ConsumerInfoModel)obj;
		}
		else {
			consumer = ConsumerDb.selectConsumerInfoByID(consumerID);
			if (consumer != null) {				
				MemcachedMgr.cachedClient.set(key, consumer);
			}
		}
		
		return consumer;
	}
	
	public static void deleteConsumerCached(int consumerID) {
		String key = keyPrefix + consumerID;
		MemcachedMgr.cachedClient.delete(key);
		/*
		ConsumerHouseCached.deleteCached(consumerID);
		ConsumerCarCached.deleteCached(consumerID);
		ConsumerPolicyCached.deleteCached(consumerID);
		ConsumerLoanCached.deleteCached(consumerID);
		ConsumerCreditCardCached.deleteCached(consumerID);
		ConsumerSelfCached.deleteCached(consumerID);
		ConsumerWorkCached.deleteCached(consumerID);
		ConsumerFollowupCached.deleteCached(consumerID);
		*/
	}
	
	
	public static Integer insertConsumerInfoObj(ConsumerInfoModel consumerInfoModel) {
		if (consumerInfoModel == null) {
			return null;
		}
		int cmrID = consumerInfoModel.getId();
		Integer res = ConsumerDb.insertConsumerInfoObj(consumerInfoModel);
		if (res != null && res > 0) {
			String key = keyPrefix + cmrID;
			MemcachedMgr.cachedClient.set(key, consumerInfoModel);
		}
		
		return res;
	}
	
	public static Integer updateConsumerInfoObj(ConsumerInfoModel consumerInfoModel, int position) {
		if (consumerInfoModel == null) {
			return null;
		}
		int cmrID = consumerInfoModel.getId();
		if (cmrID <= 0) {
			errorLog.error("updateConsumerInfoObj fail cmrID: "+cmrID);
    		return null;
		}
		ConsumerInfoModel existModel = selectConsumer(cmrID);
		if (existModel == null) {
			errorLog.error("updateConsumerInfoObj existModel is null");
    		return null;
		}
		Integer res = ConsumerDb.updateConsumerInfoObj(consumerInfoModel, existModel, position);
		if (res != null && res > 0) {
			deleteConsumerCached(cmrID);
		}
		
		return res;
	}
	
	public static List<ConsumerInfoModel> selectConsumerInfoByUserID(int userID) {
		List<Integer> idls = ConsumerDb.selectConsumerIDByUserID(userID);
		if (idls == null || idls.size() == 0) {
			return null;
		}
		List<ConsumerInfoModel> reslutls = new ArrayList<ConsumerInfoModel>();
		for (Integer cmrID : idls) {
			ConsumerInfoModel cmr = selectConsumer(cmrID);
			if (cmr != null) {
				reslutls.add(cmr);
			}
		}
		
		return reslutls;
	}
	
	public static List<ConsumerInfoModel> selectConsumerInfoByIDs(String userIDs) {
		List<Integer> idls = ConsumerDb.selectConsumerIDByIDs(userIDs);
		if (idls == null || idls.size() == 0) {
			return null;
		}
		List<ConsumerInfoModel> reslutls = new ArrayList<ConsumerInfoModel>();
		for (Integer cmrID : idls) {
			ConsumerInfoModel cmr = selectConsumer(cmrID);
			if (cmr != null) {
				reslutls.add(cmr);
			}
		}
		
		return reslutls;
	}
	
	public static List<ConsumerInfoModel> selectAllConsumerInfo() {
		List<Integer> idls = ConsumerDb.selectAllConsumerID();
		if (idls == null || idls.size() == 0) {
			return null;
		}
		List<ConsumerInfoModel> reslutls = new ArrayList<ConsumerInfoModel>();
		for (Integer cmrID : idls) {
			ConsumerInfoModel cmr = selectConsumer(cmrID);
			if (cmr != null) {
				reslutls.add(cmr);
			}
		}
		
		return reslutls;
	}
	
	public static List<ConsumerInfoModel> getConsumerInfoByIDs(List<Integer> idls) {
		List<ConsumerInfoModel> reslutls = new ArrayList<ConsumerInfoModel>();
		for (Integer cmrID : idls) {
			ConsumerInfoModel cmr = selectConsumer(cmrID);
			if (cmr != null) {
				reslutls.add(cmr);
			}
		}
		
		return reslutls;
	}
	
	public static List<ConsumerInfoModel> initAllConsumerInfoToCached() {
		List<ConsumerInfoModel> reslutls = ConsumerDb.selectAllConsumer(true);
		for (ConsumerInfoModel cmr : reslutls) {
			int cmrID = cmr.getId();
			String key = keyPrefix + cmrID;
			MemcachedMgr.cachedClient.delete(key);
			MemcachedMgr.cachedClient.set(key, cmr);
		}
		
		return reslutls;
	}
	
	public static Integer updateConsumerLeaderGuideByID(ConsumerInfoModel consumerInfoModel) {
    	Integer res = ConsumerDb.updateConsumerLeaderGuideByID(consumerInfoModel);
    	if (res != null) {
    		int consumerID = consumerInfoModel.getId();
    		String key = keyPrefix + consumerID;
    		Object obj = MemcachedMgr.cachedClient.get(key);
    		ConsumerInfoModel consumer = null;
    		if (obj != null) {
    			consumer = (ConsumerInfoModel)obj;
    			consumer.setCliLevel(consumerInfoModel.getCliLevel());
    			consumer.setLeaderGuide(consumerInfoModel.getLeaderGuide());
    			consumer.setLeaderMemo(consumerInfoModel.getLeaderMemo());
    			if (consumerInfoModel.getMeetTalkTm() != null) {
    				consumer.setMeetTalkTm(consumerInfoModel.getMeetTalkTm());
    			} else {
    				consumer.setMeetTalkTm(null);
    			}
    			MemcachedMgr.cachedClient.set(key, consumer);
    		}    		
    	}
    	
    	return res;
    }
	
	 public static void updateConsumerNameAndPhone(int consumerID,
	    		String strName, String phoneNum1) {
		 ConsumerDb.updateConsumerNameAndPhone(consumerID, strName, phoneNum1);
		 String key = keyPrefix + consumerID;
		 Object obj = MemcachedMgr.cachedClient.get(key);
		 ConsumerInfoModel consumer = null;
		 if (obj != null) {
			 consumer = (ConsumerInfoModel)obj;
			 consumer.setName(strName);
			 consumer.setPhoneNum1(phoneNum1);
			 MemcachedMgr.cachedClient.set(key, consumer);
		 }
	 }
	 
	 public static void updateConsumerStatusByID(int consumerID, int status) {
		 ConsumerDb.updateConsumerStatusByID(consumerID, status);
		 String key = keyPrefix + consumerID;
		 Object obj = MemcachedMgr.cachedClient.get(key);		 
		 if (obj != null) {
			 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
			 consumer.setStatus(status);
			 MemcachedMgr.cachedClient.set(key, consumer);
		 }
	 }
	 
	 public static Integer updateConsumerTypeByPhoneNum(String phoneNum, String cliType) {
		 List<ConsumerInfoModel> consumerls = ConsumerDb.selectConsumerByPhoneNum(phoneNum);
		 Integer res = ConsumerDb.updateConsumerTypeByPhoneNum(phoneNum, cliType);
		 if (res != null && res > 0) {
			 for (ConsumerInfoModel consumer : consumerls) {
				 int consumerID = consumer.getId();
				 String key = keyPrefix + consumerID;
				 Object obj = MemcachedMgr.cachedClient.get(key);		 
				 if (obj != null) {
					 consumer = (ConsumerInfoModel)obj;
					 consumer.setCliType(cliType);
					 MemcachedMgr.cachedClient.set(key, consumer);
				 }
			 }
		 }
		 
		 return res;
	 }
	 
	 public static Integer updateSignContractTmByPhoneNum(String phoneNum, Timestamp signTm) {
		 List<ConsumerInfoModel> consumerls = ConsumerDb.selectConsumerByPhoneNum(phoneNum);
		 Integer res = ConsumerDb.updateSignContractTmByPhoneNum(phoneNum, signTm);
		 if (res != null && res == consumerls.size()) {
			 for (ConsumerInfoModel consumer : consumerls) {
				 int consumerID = consumer.getId();
				 String key = keyPrefix + consumerID;
				 Object obj = MemcachedMgr.cachedClient.get(key);		 
				 if (obj != null) {
					 consumer = (ConsumerInfoModel)obj;
					 consumer.setSignContractTm(signTm);
					 MemcachedMgr.cachedClient.set(key, consumer);
				 }
			 }
		 }
		 
		 return res;
	 }
	 
	 public static Integer updateConsumerTypeByID(int consumerID, String cliType) {
		 Integer res = ConsumerDb.updateConsumerTypeByID(consumerID, cliType);
		 if (res != null) {    		
			 String key = keyPrefix + consumerID;
			 Object obj = MemcachedMgr.cachedClient.get(key);
			 ConsumerInfoModel consumer = null;
			 if (obj != null) {
				 consumer = (ConsumerInfoModel)obj;
				 consumer.setCliType(cliType);    			
				 MemcachedMgr.cachedClient.set(key, consumer);
			 }    		
		 }
    	
    	return res;
	 }
	 
	 public static Integer updateSignContractTmByID(int consumerID, Timestamp signTm) {
		 Integer res = ConsumerDb.updateSignContractTmByID(consumerID, signTm);
		 if (res != null) {    		
			 String key = keyPrefix + consumerID;
			 Object obj = MemcachedMgr.cachedClient.get(key);
			 ConsumerInfoModel consumer = null;
			 if (obj != null) {
				 consumer = (ConsumerInfoModel)obj;
				 consumer.setSignContractTm(signTm);			
				 MemcachedMgr.cachedClient.set(key, consumer);
			 }    		
		 }
    	
    	return res;
	 }
	 
	 public static void sendBackConsumerByID(int consumerID) {
		 ConsumerDb.sendBackConsumerByID(consumerID, 0);
		 String key = keyPrefix + consumerID;
		 Object obj = MemcachedMgr.cachedClient.get(key);		 
		 if (obj != null) {
			 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
			 consumer.setCliLevel(0);
			 consumer.setSubmitTm(null);
			 MemcachedMgr.cachedClient.set(key, consumer);
		 }
	 }

	 public static void updateConsumerUserLeaveOffice(int userID, int toUser) {
		 List<Integer> userConsumerIDs = ConsumerDb.selectUserConsumerIDs(userID);
		 ConsumerDb.updateConsumerUserLeaveOffice(userID, toUser);
		 if (userConsumerIDs != null && userConsumerIDs.size() > 0) {
			 for (Integer consumerID : userConsumerIDs) {
				 String key = keyPrefix + consumerID;
				 Object obj = MemcachedMgr.cachedClient.get(key);		 
				 if (obj != null) {
					 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
					 consumer.setUserID(toUser);
					 MemcachedMgr.cachedClient.set(key, consumer);
				 }
			 }
		 }
	 }
	 
	 public static Integer updateConsumerUserByID(int consumerID, int toUser, String setCliType) {
		 Integer res = ConsumerDb.updateConsumerUserByID(consumerID, toUser, setCliType);
		 if (res != null && res > 0) {
			 String key = keyPrefix + consumerID;
			 Object obj = MemcachedMgr.cachedClient.get(key);		 
			 if (obj != null) {
				 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
				 consumer.setUserID(toUser);
				 consumer.setCliType(setCliType);
				 MemcachedMgr.cachedClient.set(key, consumer);
			 }
		 }
		 return res;
	 }
	 
	 public static Integer updateConsumerFlowFollowByID(int consumerID, int setFlowFollow) {
		 Integer res = ConsumerDb.updateConsumerFlowFollowByID(consumerID, setFlowFollow);
		 if (res != null && res > 0) {
			 String key = keyPrefix + consumerID;
			 Object obj = MemcachedMgr.cachedClient.get(key);		 
			 if (obj != null) {
				 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
				 consumer.setFlowFollow(setFlowFollow);
				 MemcachedMgr.cachedClient.set(key, consumer);
			 }
		 }
		 
		 return res;
	 }
	 
	 public static Integer updateSendBackConsumer(int consumerID) {
		 Integer res = ConsumerDb.updateSendBackConsumer(consumerID);
		 if (res != null && res > 0) {
			 String key = keyPrefix + consumerID;
			 Object obj = MemcachedMgr.cachedClient.get(key);		 
			 if (obj != null) {
				 ConsumerInfoModel consumer = (ConsumerInfoModel)obj;
				 consumer.setFlowFollow(ConsumerInfoModel.Flow_Back);
				 consumer.setStatus(0);
				 consumer.setSubmitTm(null);
				 MemcachedMgr.cachedClient.set(key, consumer);
			 }
		 }
		 
		 return res;
	 }
	 
}
